﻿using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Repository;
using System.Linq;
using ZR.Service.IService.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model;

namespace ZR.Service.Service.ProductionControl {
    /// <summary>
    /// 报警信息配置Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ICksAlarmMessageConfigService), ServiceLifetime = LifeTime.Transient)]
    public class CksAlarmMessageConfigService : BaseService<CksAlarmMessageConfig>, ICksAlarmMessageConfigService {
        /// <summary>
        /// 报警信息配置列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<CksAlarmMessageConfigDto> GetList(CksAlarmMessageConfigQueryDto parm) {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .OrderBy(o=>o.Id, OrderByType.Desc)
                .ToPage<CksAlarmMessageConfig, CksAlarmMessageConfigDto>(parm);

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public CksAlarmMessageConfig GetInfo(int Id) {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加报警信息配置
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public CksAlarmMessageConfig AddCksAlarmMessageConfig(CksAlarmMessageConfig model) {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改报警信息配置
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateCksAlarmMessageConfig(CksAlarmMessageConfig model) {
            return Update(model, true);
        }

        /// <summary>
        /// 导入报警信息配置
        /// </summary>
        /// <returns></returns>
        public (string, object, object) ImportCksAlarmMessageConfig(List<CksAlarmMessageConfig> list) {
            var x = Context.Storageable(list)
                .SplitInsert(it => !it.Any())
                //.WhereColumns(it => it.UserName)//如果不是主键可以这样实现（多字段it=>new{it.x1,it.x2}）
                .ToStorage();
            var result = x.AsInsertable.ExecuteCommand();//插入可插入部分;

            string msg = $"插入{x.InsertList.Count} 更新{x.UpdateList.Count} 错误数据{x.ErrorList.Count} 不计算数据{x.IgnoreList.Count} 删除数据{x.DeleteList.Count} 总共{x.TotalList.Count}";
            Console.WriteLine(msg);

            //输出错误信息               
            foreach (var item in x.ErrorList) {
                Console.WriteLine("错误" + item.StorageMessage);
            }
            foreach (var item in x.IgnoreList) {
                Console.WriteLine("忽略" + item.StorageMessage);
            }

            return (msg, x.ErrorList, x.IgnoreList);
        }

        /// <summary>
        /// 导出报警信息配置
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<CksAlarmMessageConfigDto> ExportList(CksAlarmMessageConfigQueryDto parm) {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .Select((it) => new CksAlarmMessageConfigDto() {
                }, true)
                .ToPage(parm);

            return response;
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<CksAlarmMessageConfig> QueryExp(CksAlarmMessageConfigQueryDto parm) {
            var predicate = Expressionable.Create<CksAlarmMessageConfig>();
            predicate.AndIF(!string.IsNullOrEmpty(parm.Content), o => o.PlcID.ToString().Contains(parm.Content) || o.MessageCn.Contains(parm.Content) || o.MessageUs.Contains(parm.Content));
            return predicate;
        }
    }
}